//Source file: C:\\uml-generated-code\\com\\club325\\voicemicroblog\\Business\\VoiceblogControllerImplement.java

package com.club325.voicemicroblog.persist;

import java.util.ArrayList;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.club325.voicemicroblog.database.hibernateutil.HibernateUtil;
import com.club325.voicemicroblog.domain.VoiceFile;

public class VoiceblogControllerImplement implements VoiceblogControllerUtils{


	public Boolean addVoiceblog(VoiceFile VoiceBlog) {
		if(VoiceBlog == null)
			return false;
		
		try
		{
			HibernateUtil.add(VoiceBlog);
			return true;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}

	@SuppressWarnings("unchecked")
	public ArrayList<VoiceFile> showVoiceblog(String UserID) {
		if(UserID == null)
			return null;
		
		try
		{
			Criteria crit = HibernateUtil.getSession().createCriteria(VoiceFile.class);
			crit.add(Restrictions.eq("Speaker", UserID));
			return (ArrayList<VoiceFile>)crit.list();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public VoiceFile getVoiceblogById(int VoiceblogId) {
		if(VoiceblogId < 0)
		{
			return null;
		}
		
		try
		{
			Criteria crit = HibernateUtil.getSession().createCriteria(VoiceFile.class);
			crit.add(Restrictions.eq("Id", VoiceblogId));
			return (VoiceFile)crit.uniqueResult();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public VoiceblogControllerImplement() {

	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<VoiceFile> getFriendsVoiceBlogList(String UserID) {
		if(UserID == null)
			return null;
		try
		{
			String hql = "SELECT vf FROM  VoiceFile AS vf , Focus AS f WHERE vf.Speaker = f.Speaker AND f.Listener = ?" +
					" ORDER BY vf.ModifiedTime DESC";
			Query q = HibernateUtil.getSession().createQuery(hql);
			q.setString(0, UserID);
			return (ArrayList<VoiceFile>) q.list();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public ArrayList<VoiceFile> getUserVoiceBlogList(String UserID) {
		if(UserID == null)
			return null;
		try
		{
			Criteria crit = HibernateUtil.getSession().createCriteria(VoiceFile.class);
			crit.add(Restrictions.eq("Speaker", UserID));
			crit.addOrder(Order.desc("ModifiedTime"));
			return (ArrayList<VoiceFile>) crit.list();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public ArrayList<VoiceFile> getGlobalVoiceBLogList(int Page) {
		if(Page <= 0)
			return null;
		try
		{
			Criteria crit = HibernateUtil.getSession().createCriteria(VoiceFile.class);
			crit.addOrder(Order.desc("ModifiedTime"));
			crit.setMaxResults(GlobalPageLength);
			crit.setFirstResult((Page-1)*GlobalPageLength);
			return (ArrayList<VoiceFile>) crit.list();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
}
